<?php
/**
*
* @package express
* @version $Id: feed.php 9 2008-04-08 13:48:00Z nedka $
* @copyright (c) 2008 nedka (Nguyen Dang Khoa)
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$user->add_lang('mods/express');

// Get mode
$type = request_var('type', '');
$mode = request_var('mode', '');
$cat = ($type == 'express') ? request_var('cat', 0) : '';
$forum = ($type == 'board') ? request_var('forum', 0) : '';

// Check our mode...
if (!in_array($type, array('express', 'board')) || !in_array($mode, array('rss', 'atom', 'opml')))
{
	trigger_error('NO_MODE');
}

// Can this user view articles?
if (!$auth->acl_gets('u_express'))
{
	if ($user->data['user_id'] != ANONYMOUS)
	{
		trigger_error('NO_VIEW_ARTICLES');
	}

	login_box('', $user->lang['LOGIN_EXPLAIN_VIEW']);
}

// Get category name
if ($cat)
{
	$sql = 'SELECT cat_name
		FROM ' . EXPRESS_CATEGORIES_TABLE . "
		WHERE cat_id = $cat";
	$result = $db->sql_query($sql);
	$cat_name = $db->sql_fetchfield('cat_name');
	$db->sql_freeresult($result);
}

// Get forum name
if ($forum)
{
	$sql = 'SELECT forum_name
		FROM ' . FORUMS_TABLE . "
		WHERE forum_id = $forum";
	$result = $db->sql_query($sql);
	$forum_name = $db->sql_fetchfield('forum_name');
	$db->sql_freeresult($result);
}

$config['express_feed_content_limit'] = 255;

// Feed articles from the express news system
if ($type == 'express')
{
	$sql = 'SELECT article_id, article_title, article_intro, article_time, article_source
		FROM ' . EXPRESS_ARTICLES_TABLE .
		(($cat) ? " WHERE article_cat = $cat" : '') . '
		ORDER BY article_time DESC';
	$result = $db->sql_query_limit($sql, $config['express_feed_limit']);

	while ($row = $db->sql_fetchrow($result))
	{
		$template->assign_block_vars('feeds', array(
			'TITLE'		=> $row['article_title'],
//			'INTRO'		=> (utf8_strlen($row['article_intro']) > $config['express_feed_content_limit'] + 1) ? truncate_string($row['article_intro'], $config['express_feed_content_limit']) . "\xE2\x80\xA6" : $row['article_intro'],
			'INTRO'		=> $row['article_intro'],
			'TIME'		=> ($mode == 'rss') ? date('D, j M Y H:i:s', $row['article_time']) . ' +0000' : (($mode == 'atom') ? date('Y-m-j', $row['article_time']) . 'T' . date('H:i:s', $row['article_time']) . '+00:00' : ''),
			'URL'		=> $config['server_protocol'] . $config['server_name'] . $config['script_path'] .  '/express.' . $phpEx . "?cat=$cat&amp;mode=view&amp;article=" . $row['article_id'],
			'SOURCE'	=> ($row['article_source']) ? $row['article_source'] : $config['express_copyright'],
		));
	}
	$db->sql_freeresult($result);
}
// Feed posts from the board
else
{
	$sql = 'SELECT post_id, post_subject, post_text, bbcode_uid, post_time, post_username
		FROM ' . POSTS_TABLE .
		(($forum) ? " WHERE forum_id = $forum" : '') . '
		ORDER BY post_time DESC';
	$result = $db->sql_query_limit($sql, $config['express_feed_limit']);

	while ($row = $db->sql_fetchrow($result))
	{
		// Get plain text only
		$post_text_data = array(
			'text'	=> $row['post_text'],
		);

		$post_text_data = generate_text_for_edit($row['post_text'], $row['bbcode_uid'], 0);

		$template->assign_block_vars('feeds', array(
			'TITLE'		=> $row['post_subject'],
			'INTRO'		=> (utf8_strlen($post_text_data['text']) > $config['express_feed_content_limit'] + 1) ? truncate_string($post_text_data['text'], $config['express_feed_content_limit']) . "\xE2\x80\xA6" : $post_text_data['text'],
			'TIME'		=> ($mode == 'rss') ? date('D, j M Y H:i:s', $row['post_time']) . ' +0000' : (($mode == 'atom') ? date('Y-m-j', $row['post_time']) . 'T' . date('H:i:s', $row['post_time']) . '+00:00' : ''),
			'URL'		=> $config['server_protocol'] . $config['server_name'] . $config['script_path'] .  '/viewtopic.' . $phpEx . '?p=' . $row['post_id'] . '#p' . $row['post_id'],
			'SOURCE'	=> ($row['post_username']) ? $row['post_username'] : '',
		));
	}
	$db->sql_freeresult($result);
}

// Assign index specific vars
$template->assign_vars(array(
	'COPYRIGHT'		=> $config['express_copyright'],
	'BOARD_EMAIL'	=> $config['board_email'],
	'BOARD_CONTACT'	=> $config['board_contact'],
	'SITE_URL'		=> $config['server_protocol'] . $config['server_name'] . $config['script_path'],
	'FEED_URL'		=> $config['server_protocol'] . $config['server_name'] . $config['script_path'] . '/feed.' . $phpEx . '?mode=' . $mode,
	'LAST_UPDATED'	=> ($mode == 'rss') ? date('D, j M Y H:i:s', $config['express_last_updated']) . ' +0000' : (($mode == 'atom') ? date('Y-m-j', $config['express_last_updated']) . 'T' . date('H:i:s', $config['express_last_updated']) . '+00:00' : ''),

	'L_FEED_TITLE'			=> ($cat) ? (sprintf($user->lang['FEED_CAT'], $cat_name)) : (($forum) ? sprintf($user->lang['FEED_FORUM'], $forum_name) : sprintf($user->lang['FEED_TITLE'], $config['server_name'])),
	'L_FEED_TITLE_EXPLAIN'	=> ($cat) ? (sprintf($user->lang['FEED_CAT_EXPLAIN'], $cat_name)) : (($forum) ? sprintf($user->lang['FEED_FORUM_EXPLAIN'], $forum_name) : sprintf($user->lang['FEED_TITLE_EXPLAIN'], $config['server_name'])),
));

// Set page title
$page_title = ($cat) ? $user->lang['FEED_CAT'] : (($forum) ? $user->lang['FEED_FORUM'] : $user->lang['FEED_TITLE']);

// Output page
page_header($page_title);

$template->set_filenames(array(
	'body' => ($mode == 'rss') ? 'express_feed_rss.html' : (($mode == 'atom') ? 'express_feed_atom.html' : 'express_feed_opml.html'))
);

page_footer();

?>